package saucaca.fragment;

import saucaca.Util.PreferenceUtils;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ShareCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class SettingFragment extends Fragment {
	MainActivity mActivity;
	LinearLayout layoutGopy, layoutThongtin;
	ToggleButton toggleDownload, toggleVideo;
	boolean download3G = false, videoHD = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.setting, container, false);
		findViewById(view);

		download3G = PreferenceUtils.getBoolean(mActivity, TagName.KeyPreference_Download3G,true);
		videoHD = PreferenceUtils.getBoolean(mActivity, TagName.KeyPreference_VideoHD,true);
		
		toggleDownload.setChecked(download3G);
		toggleVideo.setChecked(videoHD);

		toggleDownload.setOnCheckedChangeListener(checkedChangeListener);
		toggleVideo.setOnCheckedChangeListener(checkedChangeListener);

		layoutGopy.setOnClickListener(clickListener);
		layoutThongtin.setOnClickListener(clickListener);
		mActivity.myActionBar.setMenu(R.string.menu_settings, false, true);
		return view;
	}

	OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			switch (buttonView.getId()) {
			case R.id.layout_setting_toggleVideo:
				videoHD = !videoHD;
				PreferenceUtils.putBoolean(mActivity, TagName.KeyPreference_VideoHD, videoHD);
				break;
			case R.id.layout_setting_toggleDownload:
				download3G = !download3G;
				PreferenceUtils.putBoolean(mActivity, TagName.KeyPreference_Download3G, download3G);
				break;
			default:
				break;
			}
		}
	};

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.layout_setting_layout_gopy:
				ShareCompat.IntentBuilder builder = ShareCompat.IntentBuilder.from(mActivity);
				builder.setType("message/rfc822");
				builder.addEmailTo("contact@lemonmedia.vn");
				builder.setSubject(mActivity.getString(R.string.gopy_phanhoi));
				builder.setChooserTitle("Góp ý");
				builder.startChooser();
				break;
			case R.id.layout_setting_layout_thongtin:
				((MainActivity) mActivity).switchContent(new InforFragment());
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (!hidden)
			mActivity.myActionBar.setMenu(R.string.menu_settings, false, true);
	}

	public void findViewById(View view) {
		toggleVideo = (ToggleButton) view.findViewById(R.id.layout_setting_toggleVideo);
		toggleDownload = (ToggleButton) view.findViewById(R.id.layout_setting_toggleDownload);
		layoutThongtin = (LinearLayout) view.findViewById(R.id.layout_setting_layout_thongtin);
		layoutGopy = (LinearLayout) view.findViewById(R.id.layout_setting_layout_gopy);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}
}
